# -*- coding: utf-8 -*-
import requests
import json
import time
from utils import generate_vcode
from utils.log import log
# md5 function
def md5(pwd):
    import hashlib
    m = hashlib.md5()
    m.update(pwd)
    return m.hexdigest()


url = 'http://123.56.124.18:8080/AliMNS/sms/publish'
secretkey = "908167EF3CE4BAEF"
timestamp = str(time.time())
token = md5(timestamp + secretkey)

headers = {
    'Content-Type': 'application/json;charset=utf-8',
    'timestamp': timestamp,
    'token':token
        }
def message_code(phonenumber,action_type):
    code = generate_vcode.create_verifycode()
    if action_type =='reset':
        mydata={
            "signName": "智客",
            "templateCode": "SMS_57710150",
            "templateParams": {
                "code": code,
                "product": "智客"
            },
            "phone": [phonenumber]
        }
    else:
        mydata={
            "signName": "智客",
            "templateCode": "SMS_57710152",
            "templateParams": {
                "code": code,
                "product":"智客"
            },
            "phone": [phonenumber]
        }
    r = requests.post(url, data=json.dumps(mydata),headers=headers)
    log.info("%s, %s, %s" % (phonenumber,code, r.status_code))
    return code
